

import javax.swing.*;
import javax.swing.table.JTableHeader;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.Vector;

/**
 * @author 绛河
 * 教师功能页
 */
public class FunctionTeacher {
    DatabaseConnection dc = new DatabaseConnection();
    Font font = new Font("仿宋", Font.BOLD, 20);
    Font font1 = new Font("宋体", Font.BOLD, 20);
    JButton querybutton = new JButton("查询");
    JFrame page1;
    JMenuBar menuBar;
    ImageIcon imageIcon1 = new ImageIcon("src\\practicaltraining\\1.png");
    ImageIcon imageIcon2 = new ImageIcon("src\\practicaltraining\\2.png");
    ImageIcon imageIcon3 = new ImageIcon("src\\practicaltraining\\3.png");
    ImageIcon imageIcon4 = new ImageIcon("src\\practicaltraining\\4.png");
    ImageIcon imageIcon5 = new ImageIcon("src\\practicaltraining\\6.png");
    ImageIcon imageIcon6 = new ImageIcon("src\\practicaltraining\\7.png");
    ImageIcon imageIcon7 = new ImageIcon("src\\practicaltraining\\9.png");
    JLabel textIcon1 = new JLabel(imageIcon1);
    JLabel textIcon2 = new JLabel(imageIcon2);
    JLabel textIcon3 = new JLabel(imageIcon3);
    JLabel textIcon4 = new JLabel(imageIcon4);
    JLabel textIcon5 = new JLabel(imageIcon5);
    JLabel textIcon6 = new JLabel(imageIcon6);
    JLabel textIcon7 = new JLabel(imageIcon7);


    public void teacher() {
        page1 = new JFrame("教师功能页");
        page1.setSize(700, 550);
        page1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        page1.setLocationRelativeTo(null);
        page1.setLayout(null);
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (Exception e) {
            System.out.println(e);
        }
        menuBar = new JMenuBar();
        JMenu m1 = new JMenu("成绩管理");
        JMenuItem mi1 = new JMenuItem("录入成绩");
        JMenuItem mi2 = new JMenuItem("查询成绩");
        JMenuItem mi3 = new JMenuItem("修改成绩");

        m1.add(mi1);
        m1.add(mi2);
        m1.add(mi3);

        m1.setFont(font1);

        mi1.setFont(font1);
        mi2.setFont(font1);
        mi3.setFont(font1);

        menuBar.add(m1);

        menuBar.setBounds(0, 0, 700, 80);
        textIcon1.setBounds(40, 70, 130, 120);
        textIcon2.setBounds(200, 70, 130, 120);
        textIcon3.setBounds(360, 70, 130, 120);
        textIcon4.setBounds(520, 70, 130, 120);
        textIcon5.setBounds(60, 220, 140, 130);
        textIcon6.setBounds(270, 220, 140, 130);
        textIcon7.setBounds(470, 220, 140, 130);

        page1.setJMenuBar(menuBar);
        page1.add(textIcon1);
        page1.add(textIcon2);
        page1.add(textIcon3);
        page1.add(textIcon4);
        page1.add(textIcon5);
        page1.add(textIcon6);
        page1.add(textIcon7);

        mi1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                addTheScore();
                page1.dispose();
            }
        });
        mi2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                scoreQuery();
                page1.dispose();
            }
        });
        mi3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                modifiedGrades();
            }
        });


        page1.setVisible(true);
    }

    JFrame page2;
    JLabel textIcon8, textIcon9, textIcon10, textIcon11, textIcon12;
    JTextField entryField1, entryField2, entryField3, entryField4, entryField5;
    JButton button1;

    public void addTheScore() {
        page2 = new JFrame("添加成绩");
        page2.setSize(320, 450);//设置窗体大小
        page2.setLocationRelativeTo(null);//设置窗口每次启动都显示在屏幕正中央
        page2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        page2.setLayout(null);
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (Exception e) {
            System.out.println(e);
        }
        textIcon8 = new JLabel("学  号：");
        textIcon8.setBounds(10, 10, 100, 40);
        textIcon8.setFont(font);
        entryField1 = new JTextField(null, 20);
        entryField1.setBounds(90, 10, 180, 30);
        entryField1.setFont(font);
        textIcon9 = new JLabel("姓  名：");
        textIcon9.setBounds(10, 50, 100, 40);
        textIcon9.setFont(font);
        entryField2 = new JTextField(null, 20);
        entryField2.setBounds(90, 50, 180, 30);
        entryField2.setFont(font);
        textIcon10 = new JLabel("课程号：");
        textIcon10.setBounds(8, 90, 100, 40);
        textIcon10.setFont(font);
        entryField3 = new JTextField(null, 20);
        entryField3.setBounds(90, 90, 180, 30);
        entryField3.setFont(font);
        textIcon11 = new JLabel("课程名：");
        textIcon11.setBounds(8, 130, 100, 40);
        textIcon11.setFont(font);
        entryField4 = new JTextField(null, 20);
        entryField4.setBounds(90, 130, 180, 30);
        entryField4.setFont(font);
        textIcon12 = new JLabel("成  绩：");
        textIcon12.setBounds(8, 170, 100, 40);
        textIcon12.setFont(font);
        entryField5 = new JTextField(null, 20);
        entryField5.setBounds(90, 170, 180, 30);
        entryField5.setFont(font);
        button1 = new JButton("添加成绩信息");
        button1.setBounds(65, 230, 180, 40);
        button1.setFont(font);
        //给查询按钮绑定一个监听点击事件
        button1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                PreparedStatement st = null, st1 = null, st2 = null;
                ResultSet rs = null, rs2 = null;
                Connection con = null;
                try {
                    con = dc.getConnection();

                    String sql1 = "select * from student_message";
                    String sql2 = "select * from course_arrangement";
                    String sql3 = "insert into grade(student_number,student_name,course_number,course_name,student_class,score) values(?,?,?,?,?,?)";
                    String data1 = null, data2 = null;
                    st = con.prepareStatement(sql1);
                    st2 = con.prepareStatement(sql3);
                    st1 = con.prepareStatement(sql2);
                    rs = st.executeQuery();
                    boolean flag1 = false;
                    while (rs.next()) {
                        if (rs.getString(1).equals(entryField1.getText())) {
                            data1 = rs.getString(3);
                            flag1 = true;
                        }
                    }
                    rs2 = st1.executeQuery();
                    boolean flag2 = false;
                    while (rs2.next()) {
                        if (rs2.getString(1).equals(data1)) {
                            if (rs2.getString(3).equals(entryField3.getText())) {
                                data2 = rs2.getString(5);
                                flag2 = true;
                            }
                        }
                    }
                    boolean flag3 = false;
                    if (Register.getLoginInformation().equals(data2)) {
                        flag3 = true;
                    }
                    System.out.println(flag1);
                    System.out.println(flag2);
                    System.out.println(flag3);
                    if (flag1 && flag2 && flag3) {
                        st2.setString(1, entryField1.getText());
                        st2.setString(2, entryField2.getText());
                        st2.setString(3, entryField3.getText());
                        st2.setString(4, entryField4.getText());
                        st2.setString(5, data1);
                        st2.setString(6, entryField5.getText());
                        st2.executeUpdate();
                        JOptionPane.showMessageDialog(null, "添加成功");
                        entryField1.setText(null);
                        entryField2.setText(null);
                        entryField3.setText(null);
                        entryField4.setText(null);
                        entryField5.setText(null);

                    } else {
                        JOptionPane.showMessageDialog(null, "添加失败");
                        entryField1.setText(null);
                        entryField2.setText(null);
                        entryField3.setText(null);
                        entryField4.setText(null);
                        entryField5.setText(null);
                    }
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }

            }
        });
        page2.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                teacher();
            }
        });
        //将这些按钮和文本等加入到窗体中
        page2.add(textIcon8);
        page2.add(entryField1);
        page2.add(textIcon9);
        page2.add(entryField2);
        page2.add(textIcon10);
        page2.add(entryField3);
        page2.add(textIcon11);
        page2.add(entryField4);
        page2.add(textIcon12);
        page2.add(entryField5);
        page2.add(button1);
        page2.setVisible(true);
    }

    JFrame page3;
    JTextField entryField6;
    JLabel textIcon13;
    JScrollPane scrollPane1;
    JTable tabulation1;
    JTableHeader header1;
    JButton alter;

    public void scoreQuery() {
        page3 = new JFrame("成绩查询");
        page3.setSize(850, 700);
        page3.setLocationRelativeTo(null);//设置窗口每次启动都显示在屏幕正中央
        page3.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        page3.setLayout(null);
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (Exception e) {
            System.out.println(e);
        }
        Font font = new Font("仿宋", Font.BOLD, 20);
        textIcon13 = new JLabel("班级号：");
        textIcon13.setBounds(160, 50, 150, 30);
        textIcon13.setFont(font);

        entryField6 = new JTextField(null, 20);
        entryField6.setBounds(290, 50, 180, 30);
        entryField6.setFont(font);
        querybutton.setBounds(500, 50, 80, 30);
        querybutton.setFont(font);
        alter = new JButton("修改");
        alter.setBounds(600, 50, 80, 30);
        alter.setFont(font);


        scrollPane1 = new JScrollPane();
        scrollPane1.setBounds(50, 100, 700, 450);

        querybutton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                Connection con = null;
                ResultSet rs, rs1, rs2, rs3, rs4;
                PreparedStatement ps, ps1;
                try {
                    con = dc.getConnection();
                    String sql = "select * from grade";
                    String sql1 = "select * from course_arrangement";
                    ps = con.prepareStatement(sql);
                    ps1 = con.prepareStatement(sql1);
                    rs1 = ps1.executeQuery();
                    String data = null;
                    int count1 = 0, count2 = 0;
                    Vector<String> number = new Vector<>();
                    while (rs1.next()) {
                        if (Register.getLoginInformation().equals(rs1.getString(5))) {
                            data = rs1.getString(4);//记录教授的课程名
                            number.add(rs1.getString(1));//记录教授的班级
                        }
                    }
                    if (entryField6.getText().length() == 0) {
                        rs = ps.executeQuery();
                        while (rs.next()) {
                            if (rs.getString(4).equals(data)) {
                                for (String string : number) {
                                    if (rs.getString(5).equals(string)) {
                                        count1++;
                                    }
                                }
                            }
                        }
                        Object[][] info = new Object[count1][6];
                        count1 = 0;
                        rs2 = ps.executeQuery();
                        while (rs2.next()) {
                            if (rs2.getString("course_name").equals(data)) {
                                for (String string : number) {
                                    if (rs2.getString(5).equals(string)) {
                                        info[count1][0] = rs2.getString("student_number");
                                        info[count1][1] = rs2.getString("student_name");
                                        info[count1][2] = rs2.getString("course_number");
                                        info[count1][3] = rs2.getString("course_name");
                                        info[count1][4] = rs2.getString("student_class");
                                        info[count1][5] = rs2.getFloat("score");
                                        count1++;
                                    }
                                }
                            }
                        }
                        String[] title = {"学号", "姓名", "课程号", "课程名", "班级", "成绩"};
                        tabulation1 = new JTable(info, title);
                        header1 = tabulation1.getTableHeader();
                        scrollPane1.getViewport().add(tabulation1);
                        tabulation1.getTableHeader().setPreferredSize(new Dimension(1, 30));
                        tabulation1.getTableHeader().setFont(font);
                        tabulation1.setRowHeight(40);
                        tabulation1.setFont(new Font("Dialog", 0, 20));
                    }
                    boolean flag = false;
                    if (entryField6.getText().length() != 0) {
                        rs = ps.executeQuery();
                        while (rs.next()) {
                            for (String string : number) {
                                if (entryField6.getText().equals(string)) {
                                    flag = true;
                                }
                            }
                        }
                        if (flag) {
                            rs3 = ps.executeQuery();
                            while (rs3.next()) {
                                if (rs3.getString("course_name").equals(data)
                                        && rs3.getString(5).equals(entryField6.getText())) {
                                    count2++;
                                }
                            }
                        }
                        Object[][] info = new Object[count2][6];
                        count2 = 0;
                        rs4 = ps.executeQuery();
                        while (rs4.next()) {
                            if (rs4.getString("course_name").equals(data)
                                    && rs4.getString(5).equals(entryField6.getText())) {
                                info[count2][0] = rs4.getString("student_number");
                                info[count2][1] = rs4.getString("student_name");
                                info[count2][2] = rs4.getString("course_number");
                                info[count2][3] = rs4.getString("course_name");
                                info[count2][4] = rs4.getString("student_class");
                                info[count2][5] = rs4.getFloat("score");
                                count2++;
                            }
                            String[] title = {"学号", "姓名", "课程号", "课程名", "班级", "成绩"};
                            tabulation1 = new JTable(info, title);
                            header1 = tabulation1.getTableHeader();
                            scrollPane1.getViewport().add(tabulation1);
                            tabulation1.getTableHeader().setPreferredSize(new Dimension(1, 30));
                            tabulation1.getTableHeader().setFont(font);
                            tabulation1.setRowHeight(40);
                            tabulation1.setFont(new Font("Dialog", 0, 20));
                        }
                    }
                } catch (SQLException ex) {
                    ex.printStackTrace();
                } finally {
                    if (con != null) {
                        try {
                            con.close();
                        } catch (SQLException throwables) {
                            throwables.printStackTrace();
                        }
                    }
                }
            }
        });
        alter.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int i = tabulation1.getSelectedRow();
                String data = String.valueOf(tabulation1.getValueAt(i, 0));
                String data1 = String.valueOf(tabulation1.getValueAt(i, 2));
                entryField7.setText(data);
                entryField8.setText(data1);
                modifiedGrades();
                page4.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            }
        });


        page3.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                teacher();
            }
        });
        page3.add(querybutton);
        page3.add(entryField6);
        page3.add(textIcon13);
        page3.add(scrollPane1);
        page3.add(alter);
        page3.setVisible(true);
    }

    JFrame page4;
    JLabel textIcon14, textIcon15, textIcon16;
    JTextField entryField7 = new JTextField(null, 20);
    JTextField entryField8 = new JTextField(null, 20);
    JTextField entryField9 = new JTextField(null, 20);
    JButton button2;

    public void modifiedGrades() {
        page4 = new JFrame("修改成绩");
        //设置窗体大小
        page4.setSize(320, 280);
        //设置窗口每次启动都显示在屏幕正中央
        page4.setLocationRelativeTo(null);
        page4.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        page4.setLayout(null);
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (Exception e) {
            System.out.println(e);
        }
        textIcon14 = new JLabel("学  号：");
        textIcon14.setBounds(10, 10, 100, 40);
        textIcon14.setFont(font);
        entryField7.setBounds(90, 15, 180, 30);
        entryField7.setFont(font);
        textIcon15 = new JLabel("课程号：");
        textIcon15.setBounds(8, 50, 100, 40);
        textIcon15.setFont(font);
        entryField8.setBounds(90, 55, 180, 30);
        entryField8.setFont(font);
        textIcon16 = new JLabel("成  绩：");
        textIcon16.setBounds(10, 90, 100, 40);
        textIcon16.setFont(font);
        entryField9.setBounds(90, 95, 180, 30);
        entryField9.setFont(font);
        button2 = new JButton("修改成绩信息");
        button2.setBounds(65, 140, 180, 40);
        button2.setFont(font);
        button2.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                PreparedStatement ps;//PreparedStatement==>表示预编译的SQL语句的对象。
                // SQL语句已预编译并存储在PreparedStatement对象中。
                Connection con;//Connection==>与特定数据库的连接
                Statement st;
                ResultSet rs;
                try {
                    con = dc.getConnection();
                    String sql1 = "update grade set score=? where student_number=? and course_number=?";
                    ps = con.prepareStatement(sql1);
                    String sql2 = "select * from grade";
                    st = con.createStatement();
                    rs = st.executeQuery(sql2);

                    int flag = 0;
                    while (rs.next()) {
                        //根据学号和课程名修改成绩
                        if (rs.getString("student_number").equals(entryField7.getText()) && rs.getString("course_number").equals(entryField7.getText())) {
                            ps.setString(1, entryField9.getText());
                            ps.setString(2, entryField7.getText());
                            ps.setString(3, entryField8.getText());
                            flag++;
                        }
                    }
                    if (flag == 0) {
                        JOptionPane.showMessageDialog(null, "没有该学生和课程！");
                    }
                    if (entryField7.getText().length() != 0 && entryField8.getText().length() != 0
                            && entryField9.getText().length() != 0 && flag != 0) {
                        JOptionPane.showMessageDialog(null, "修改成功！");
                        entryField7.setText(null);
                        entryField8.setText(null);
                        entryField9.setText(null);
                        page4.dispose();
                        try {
                            ps.executeUpdate();//将数据存储到数据库中
                        } catch (SQLException ex) {
                            ex.printStackTrace();
                        }
                    } else {
                        JOptionPane.showMessageDialog(null, "修改失败！");
                    }
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        });

        //将这些按钮和文本等加入到窗体中
        page4.add(textIcon14);
        page4.add(entryField9);
        page4.add(textIcon15);
        page4.add(entryField7);
        page4.add(textIcon16);
        page4.add(entryField8);
        page4.add(button2);
        page4.setVisible(true);
    }

}
